/**
Vector3.cpp
Title: Binge Vector3 Program File
Descr: Vector3 Class

Copyright (c) 2010, Brant Ryan Anderson
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <ORGANIZATION> nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/

#include "../../binge/utilities/Vector3.h"

namespace binge {

Vector3::Vector3(void)
{
	x = y = z = 0;
}

Vector3::Vector3( const Vector3& v )
{
	*this = v;
}

Vector3::Vector3( float x, float y, float z )
{
	Set( x, y, z );
}

Vector3::Vector3( int x, int y, int z)
{
        Set((float)x,(float)y,(float)z);
}

void
Vector3::Set( float x1,float y1,float z1 )
{
	x=x1; y=y1; z=z1;
}

void
Vector3::Set( const Vector3& v)
{
	x=v.x; y=v.y; z=v.z;
}

void
Vector3::Move( float mx,float my,float mz)
{
	x+=mx; y+=my; z+=mz;
}

void
Vector3::operator+=(const Vector3& v)
{
	x+=v.x; y+=v.y; z+=v.z;
}

void
Vector3::operator-=(const Vector3& v)
{
	x-=v.x; y-=v.y; z-=v.z;
}

void
Vector3::operator*=(const Vector3& v)
{
	x*=v.x; y*=v.y; z*=v.z;
}

void
Vector3::operator/=(const Vector3& v)
{
	x/=v.x; y/=v.y; z/=v.z;
}

bool //equality operator comparison includes double rounding
Vector3::operator==( const Vector3& v ) const
{
	return (
		(((v.x - 0.0001f) < x) && (x < (v.x + 0.0001f))) &&
		(((v.y - 0.0001f) < y) && (y < (v.y + 0.0001f))) &&
		(((v.z - 0.0001f) < z) && (z < (v.z + 0.0001f))) );
}

bool //inequality operator
Vector3::operator!=( const Vector3& p ) const
{
	return (!(*this == p));
}

Vector3& //assign operator
Vector3::operator=( const Vector3& v)
{
	Set(v);
	return *this;
}

float //distance only coded for 2D
Vector3::Distance( const Vector3& v )
{
	return sqrt((v.x-x)*(v.x-x) + (v.y-y)*(v.y-y));
}

float //Vector3 length is distance from the origin
Vector3::Length(void)
{
	return sqrt(x*x + y*y + z*z);
}

float //dot/scalar product: difference between two directions
Vector3::DotProduct( const Vector3& v )
{
	return (x*v.x + y*v.y + z*v.z);
}

Vector3 //cross/Vector product is used to calculate the normal
Vector3::CrossProduct( const Vector3& v )
{
        float nx = (y*v.z)-(z*v.y);
        float ny = (z*v.y)-(x*v.z);
        float nz = (x*v.y)-(y*v.x);
	return Vector3(nx,ny,nz);
}

Vector3 //calculate normal angle of the Vector
Vector3::Normal(void)
{
        float length;
	if (Length() == 0)
		length = 0;
	else
		length = 1 / Length();
        float nx = x*length;
        float ny = y*length;
        float nz = z*length;
	return Vector3(nx,ny,nz);
}

} // namespace
